# fast_count 快速统计
def fast_count(keywords):
    """
    输入随机字符串，统计每个元素出现的次数

    :param keywords: "ababbbb"
    :return:    'a':2, 'b':5
    """
    res = {}
    for i in keywords:
        res[i] = keywords.count(i)
    print("每个元素出现的次数如下：")
    print(res)
    return res


def topthree(keywords):
    """
    输入随机字符串，降序统计每个元素出现的次数，取前三位
    :param keywords:
    """
    n = 3  # 取前几位
    res = fast_count(keywords)
    # 降序排序
    lis = sorted(res.items(), key=lambda x: x[1], reverse=True)
    top3_list = lis[:n]
    # print(top3_list)
    top3_dict = {}
    for j in top3_list:
        top3_dict[j[0]] = j[1]
    print("前三位如下：")
    print(top3_dict)


if __name__ == '__main__':
    instr = input("请输入随机字符串：")
    topthree(instr)

# 完成的不错，输出可以优化下
